package org.adream.account.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.adream.account.model.ResultModel;
import org.adream.account.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.mp.api.WxMpService;

@Controller
@RequestMapping("wx")
public class WxController {
	
	@Autowired
	WxMpService wxMpService;
	
	@Autowired
	WxService service;
	
	@RequestMapping(value = "/getInfoMation")
	@ResponseBody
	public ResultModel<?> getInfoMation(@RequestBody String json) throws Exception{
		JSONObject jsonObject = JSONObject.parseObject(json);
		String url = jsonObject.getString("url");
		if(StringUtils.isEmpty(url)||StringUtils.isEmpty(url.trim())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "参数丢失", null);
		}
		WxJsapiSignature  wxJsapiSignature = wxMpService.createJsapiSignature(url);
		return new ResultModel<WxJsapiSignature>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, wxJsapiSignature);
	}
	
	@RequestMapping(value = "/login")
	public ModelAndView loginByWeixin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String code = request.getParameter("code");
		try {
			String r =  service.wxLogin(request, response, code);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("");
	}
}
